package com.tbocek.android.ode.controller;
import java.util.List;

import com.google.common.collect.Lists;
import com.tbocek.android.ode.solver.SystemState;

import android.os.AsyncTask;

/**
 * Wraps an AsyncTask.  Different AsyncTasks will require different arguments,
 * which breaks subclassing because of templating, so we use a class hierarchy
 * to adapt them.
 * @author Tim
 *
 */
public class ComputationTask 
		extends AsyncTask<List<ComputationSpec>, 
						  Integer, 
						  List<List<SystemState>>> {
	
	private Controller mController;

	public ComputationTask(Controller controller) {
		mController = controller;
	}

	@Override
	protected List<List<SystemState>> doInBackground(
			List<ComputationSpec>... params) {
		List<ComputationSpec> computations = params[0];
		List<List<SystemState>> result = Lists.newArrayList();
		
		for (ComputationSpec spec: computations){
			result.add(spec.solve());
		}
		
		return result;
	}
	
	@Override
	protected void onPostExecute(List<List<SystemState>> result) {
		if (result.size() == 1) {
			mController.notifySolution(result.get(0));
		} else {
			mController.notifySweep(result);
		}
	}
	

}